﻿using ProjectCore.Application.AppUser.Dto;
using ProjectCore.Common;
using ProjectCore.Domain.DomainService;
using ProjectCore.Domain.Model.Entity;
using ProjectCore.Domain.Repository.Interfaces;
using ProjectCore.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace ProjectCore.Application
{
    public class AppOrder : IAppOrder
    {
        private readonly IBaseRepository<UserOrderPro> baseRepository;
        private readonly MyContext myContext;
        private readonly IOrderDomainService order1;
        public AppOrder(IBaseRepository<UserOrderPro> _baseRepository, MyContext _myContext, IOrderDomainService _eval1)
        {
            baseRepository = _baseRepository;
            myContext = _myContext;
            order1 = _eval1;
        }

        //反填
        public async Task<Order> GetOrder(int Id)
        {
            return await order1.GetOrder(Id);
        }


        //获取订单集合
        public async Task<HeaderResult<List<UserOrderPro>>> GetOrders(int SendId, DateTime? Begin, DateTime? End, string Na, int pageIndex, int pageSize)
        {
            try
            {
                var l = from a in myContext.Orders
                        join b in myContext.Users
                        on a.UserID equals b.UserID
                        join c in myContext.Products
                        on a.ProId equals c.ProID
                        join d in myContext.PicUrls
                        on c.Photo equals d.ID
                        where (SendId == 0 ? true : a.Send.Equals(SendId)) && (Begin == null ? true : Begin > a.OrderTime) && (End == null ? true : End < a.OrderTime) && (Na.Length == 0 ? true : a.OrderNum.Contains(Na) || b.UserName.Contains(Na))
                        select new UserOrderPro
                        {
                            OrderNum = a.OrderNum,
                            Sort = a.Sort,
                            Pay = a.Pay,
                            Send = a.Send,
                            State = a.State,
                            OrderTime = a.OrderTime,
                            Photo = d.Photo,
                            ProName = c.ProName,
                            UserName = b.UserName
                        };

                int coun = l.ToList().Count();
                l = l.Take((pageIndex - 1) * pageSize).Skip(pageSize);
                HeaderResult<List<UserOrderPro>> result = new HeaderResult<List<UserOrderPro>>()
                {
                    IsSucceed = true,
                    Result = await l.ToListAsync(),
                    Total = coun,
                };
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }


        //支付反填
        public async Task<Pay> GetPay(int Id)
        {
            return await order1.GetPay(Id);
        }


        //收货反填
        public async Task<Site> GetSite(int Id)
        {
            return await order1.GetSite(Id);
        }


        //批量发货
        public async Task<int> UptState(string Id)
        {
            return await order1.UptState(Id);
        }
    }
}
